package cn.okcode.slowjson.ast;

import cn.okcode.slowjson.parser.Visitor;

import java.util.ArrayList;
import java.util.List;

/**
 * @author pyk13
 */
public class ArrayNode extends AbstractNode<List<?>> {

    /**
     * 存放数组的元素
     */
    private final List<AbstractNode<?>> elements;

    public List<AbstractNode<?>> getElements() {
        return elements;
    }

    public ArrayNode(List<AbstractNode<?>> childNodes) {
        this.elements = new ArrayList<>(childNodes.size());
        this.elements.addAll(childNodes);
        for (AbstractNode<?> node : childNodes) {
            node.parent = this;
        }
    }

    @Override
    public List<Object> getValue() {
        List<Object> values = new ArrayList<>(this.elements.size());
        for (AbstractNode<?> childNode : this.elements) {
            values.add(childNode.getValue());
        }
        return values;
    }

    @Override
    public String stringValue() {
        StringBuilder builder = new StringBuilder(elements.size() * 10);
        for (AbstractNode<?> childNode : this.elements) {
            builder.append(childNode.stringValue());
        }
        return builder.toString();
    }

    @Override
    public void accept(Visitor visitor) {
        visitor.visit(this);
    }
}
